package Datastruct.Clue;

//线索二叉树
public class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    /**
     * 1.如果是0表示左子树
     * 2.如果是1表示前驱
     */
    private int noleft;

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRiight() {
        return right;
    }

    public void setRiight(HeroNode riight) {
        this.right = riight;
    }

    public int getNoleft() {
        return noleft;
    }

    public void setNoleft(int noleft) {
        this.noleft = noleft;
    }

    public int getNoright() {
        return noright;
    }

    public void setNoright(int noright) {
        this.noright = noright;
    }

    /**
     * 1.如果是0表示右子树
     * 2.如果是1表示后继
     */
    private int noright;


    /**
     * 删除结点
     * 删除的可能是叶子结点，也可能是非叶子结点
     **/
    public void delNode(int no) {
        //1.删除的是当前结点的左孩子

        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        //2.删除的是当前结点是右孩子
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }


        //3.删除的不是当前结点的左孩子，向左递归删除
        if (this.left != null) {
            this.left.delNode(no);
        }

        //4.删除的不是当前结点的右孩子，向右递归删除
        if (this.right != null) {
            this.right.delNode(no);
        }
    }

    //前序遍历所有
    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    //中序遍历所有

    public void inmOrder() {
        if (this.left != null) {
            this.left.inmOrder();
        }
        System.out.println(this);

        if (this.right != null) {
            this.right.inmOrder();
        }
    }

    //后序遍历所有
    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }

        System.out.println(this);

    }

    //按编号前序查找
    public HeroNode preSearch(int no) {
        if (this.no == no) {
            return this;
        }
        HeroNode node = null;
        if (this.left != null) {
            node = this.left.preSearch(no);
        }
        if (node != null) {
            return node;
        }
        if (this.right != null) {
            node = this.right.preSearch(no);
        }

        return node;
    }

    //按编号中序查找

    public HeroNode inmSearch(int no) {
        HeroNode node = null;
        if (this.left != null) {
            node = this.left.inmSearch(no);
        }
        if (node != null) {
            return node;
        }
        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            node = this.right.inmSearch(no);
        }

        return node;
    }

    //按编号后序查找
    public HeroNode postSelect(int no) {
        HeroNode node = null;
        if (this.left != null) {
            node = this.left.postSelect(no);
        }
        if (node != null) {
            return node;
        }
        if (this.right != null) {
            node = this.right.postSelect(no);
        }
        if (node != null) {
            return node;
        }
        if (this.no == no) {
            return this;
        }
        return node;
    }


}
